/**
 * 分类信息功能
 */
import { ref } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import { findTopCategory } from '@/api/category'

export function useCategoryData () {
  const categoryData = ref({})
  const route = useRoute()
  async function getcategoryData (id = route.params.id) {
    const res = await findTopCategory(id)
    categoryData.value = res.result
  }
  getcategoryData()

  // 方案二；解决路由缓存
  onBeforeRouteUpdate(to => {
    getcategoryData(to.params.id)
  })
  return {
    categoryData
  }
}
